package com.example.demo.zp.controller;

import com.example.demo.zp.annotation.RedisCache;
import com.example.demo.zp.annotation.Trace;
import com.example.demo.zp.service.TestService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by ZhangPing on 2018/2/10.
 */
@RestController
public class HelloController {

    private final Logger logger = Logger.getLogger(HelloController.class);

    @Autowired
    private TestService testService;

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name", required = true) String name) {
        String result = "hello  " + name;
        System.out.println(result);
        return result;
    }

    @RequestMapping(value = "/world")
    public String world(@RequestParam(value = "arg", required = true) String arg) {
        String result = "world  " + arg;
        System.out.println(result);
        return result;
    }

    @RequestMapping(value = "/annotation")
    @RedisCache(type = String.class)
    public String annotation() {
        return "annotation";
    }

    @RequestMapping(value = "/trace")
    @Trace(processName = "查询订单详情", startStep = true)
    public String testTrace(@RequestParam(value = "name", required = true) String name) throws InterruptedException {
        return testService.doTest(name);
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index() {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/index,host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
        return  "Hello World";
    }
}
